package com.web.new12306.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.web.new12306.pojo.Page;
import com.web.new12306.pojo.Page1;
import com.web.new12306.pojo.Pager;
import com.web.new12306.pojo.Pager1;
import com.alibaba.fastjson.JSON;
import com.web.new12306.pojo.Order;
import com.web.new12306.pojo.Ticket;
import com.web.new12306.service.OrderService;
import com.web.new12306.service.impl.OrderServiceImpl;

/**
 * Servlet implementation class TicketServlet
 */
public class TicketServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TicketServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String action=request.getParameter("action");
		if("query".equals(action)) {
			doQuery(request,response);
		}
		if("reserve".equals(action)) {
			doAddOrder(request,response);
		}
	}

	

	private void doAddOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("utf-8");
		String ticketId1=request.getParameter("ticketId");
		OrderService os=new OrderServiceImpl();
		Ticket ticket=os.getTicketByTicketId(Integer.parseInt(ticketId1));
			
		PrintWriter out = response.getWriter();
		HttpSession session=request.getSession();
		int ticketId=ticket.getTicketId();
		String trainNumber=ticket.getTrainNumber();
		String startStation=ticket.getStartStation();
		String terminalStation=ticket.getTerminalStation();
		Timestamp time=ticket.getTime();
		//Timestamp arrivalTime=ticket.getArrivalTime();
		int price = ticket.getPrice();
		
		Order order=new Order();
		order.setUsername("hllhll");
		order.setTrainNumber(trainNumber);
		order.setStartStation(startStation);
		order.setTerminalStation(terminalStation);
		order.setTime(time);
		//order.setArrivalTime(arrivalTime);
		order.setPrice(price);

		int re=os.addOrder(order);
		if(0!=re) {
			//int num=os.updateTickets();
			//System.out.println(num);
			out.write("<script language='javascript'>alert('生成了新的订单');window.location='http://localhost:8080/GoodProject/user/success.jsp';</script>");
			
		}else {
			out.write("<script language='javascript'>alert('没有生成新订单');</script>");
		}
		response.sendRedirect("OrderSuccess.jsp");
	}

	
	private void doQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		String startStation=request.getParameter("startStation");
		String terminalStation=request.getParameter("terminalStation");
		String time=request.getParameter("time");
		System.out.println(startStation);
		System.out.println(terminalStation);
		System.out.println(time);
		
		SimpleDateFormat date=new SimpleDateFormat("yyyy-MM-dd");
		Timestamp time1=null;
		Page1 p=null;
		if(time!=null&&!("").equals(time)) {
			try {
				java.util.Date t=date.parse(time);
				//time=Timestamp(java.util.Date().getTime());
				 time1=new Timestamp(t.getTime());
				 String ts=time.toString();
				 System.out.println(ts);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			Ticket ticket=new Ticket();
			ticket.setStartStation(startStation);
			ticket.setTerminalStation(terminalStation);
			ticket.setTime(time1);
			
			System.out.println(ticket.getTime());
			
			Integer page=Integer.valueOf(request.getParameter("page"));
			Integer rows=Integer.valueOf(request.getParameter("rows"));
			System.out.println(page);
			System.out.println(rows);
			OrderService ods=new OrderServiceImpl();
			List<Ticket> list=null;
			int total=0;
			p=ods.showTickets(ticket, page, rows);
			list=p.getObjs();
			total=p.getTotalCount();
			System.out.println(total);
			
			Pager1 pager=new Pager1();
			pager.setRows(list);
			pager.setTotal(total);
			response.setContentType("application/json;charset=UTF-8");
			response.getWriter().append(JSON.toJSONString(pager));
//			
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
